package com.cckj.projectapi.demo.service;

import com.cckj.projectapi.demo.controller.Entity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DemoServiceImpl implements DemoService {

    @Override
    @Cacheable(value = "test",key = "#o")
    public Object cacheable(Object o) {
        log.info("走入缓存");
        return getEntity();
    }

    @Override
    @Cacheable(value = "test",key = "#o")
    public Object getCacheable(String o) {
        log.info("走入缓存");
        return getEntity();
    }

    @Override
    @CachePut(value = "test",key = "#o")
    public Object cachePut(Object o) {
        log.info("走入缓存");

        Entity entity = new Entity();
        entity.setName(o.toString());
        entity.setAge("29");
        return entity;
    }

    @Override
    @CacheEvict(value = "test",beforeInvocation = true)
    public Object cacheEvict(Object o) {
        log.info("走入缓存");

        String a = null;
        System.out.println(a.length());
        return null;
    }

    private Object getEntity(){
        Entity entity = new Entity();
        entity.setName("name");
        entity.setAge("28");
        return entity;
    }

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
//    @Async
    public void asyn() {
        threadPoolTaskExecutor.submit( () -> {
            System.out.println(Thread.currentThread().getName());
            try {
               Thread.sleep(5*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
//        System.out.println(Thread.currentThread().getName());
//        try {
//            Thread.sleep(5*1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
